using System.Collections.Generic;
using QFramework;
using QFramework.Game;
using QGameFramework.Models;

namespace QGameFramework.UITool
{
    public class UIManagerComponent : BaseComponent
    {
        public List<string> UIList = new List<string>();

        public override void OnInit()
        {
            base.OnInit();
            LogKit.I("UI 组件初始化");
            UIList = new List<string>();
            UIKit.Root.SetResolution(this.GetModel<IGameConfig>().UIResolution.x, this.GetModel<IGameConfig>().UIResolution.y, 0.5f);
        }

        /// <summary>
        /// 打开UI面板
        /// </summary>
        /// <param name="uiData">需要传入的数据</param>
        /// <param name="level">UI面板的层级</param>
        /// <typeparam name="T">UI面板</typeparam>
        public void OpenForm<T>(IUIData uiData = null, UILevel level = UILevel.Common) where T : UIPanel
        {
            if (!UIList.Contains(typeof(T).Name))
            {
                LogKit.I($"打开面板：{typeof(T).Name}");
                UIKit.OpenPanelAsync<T>(level, uiData).ToAction().Start(this);
                UIList.Add(typeof(T).Name);
            }
            else
            {
                UIKit.ClosePanel<T>();
                LogKit.I($"重新打开面板：{typeof(T).Name}");
                UIKit.OpenPanelAsync<T>(level, uiData).ToAction().Start(this);
            }
        }

        /// <summary>
        /// 关闭面板
        /// </summary>
        /// <typeparam name="T">UI面板</typeparam>
        public void CloseForm<T>() where T : UIPanel
        {
            if (UIList.Contains(typeof(T).Name))
            {
                UIKit.ClosePanel<T>();
                UIList.Remove(typeof(T).Name);
            }
        }

        /// <summary>
        /// UI面板入栈
        /// </summary>
        /// <typeparam name="T">UI面板</typeparam>
        public void PushForm2Stack<T>() where T : UIPanel
        {
            UIKit.Stack.Push<T>();
        }

        /// <summary>
        /// UI面板入栈
        /// </summary>
        /// <param name="t">UI面板</param>
        /// <typeparam name="T">UI面板</typeparam>
        public void PushForm2Stack<T>(T t) where T : UIPanel
        {
            UIKit.Stack.Push(t);
        }

        /// <summary>
        /// UI面板出栈
        /// </summary>
        public void PopForm()
        {
            UIKit.Stack.Pop();
        }

        /// <summary>
        /// 关闭所有UI面板
        /// </summary>
        public void CloseAllForm()
        {
            UIKit.CloseAllPanel();
            UIList.Clear();
        }


        public override void DeInit()
        {
            base.DeInit();
            UIList.Clear();
        }
    }
}